﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGCourt.Logic.Common;
using LGCourt.Logic.NBL.DBEntities;
using LGCourt.Logic.NBL;
using NBearLite;
using KMFrame.Util;
using KMFrame.Core;

namespace LGCourt.Logic.BLL
{
    public class PreCase : LGCourtLogic
    {
        /// <summary>
        /// 案件受理范围
        /// </summary>
        /// <returns></returns>
        //public pPreCaseAccepRange GetAccepRangeInfo(CaseAccepRangeParam param)
        //{
        //    return NBLData
        //        .Select(LGCourtColumns.pPreCaseAccepRange)
        //        .Where(LGCourtColumns.pPreCaseAccepRange.CaseType == param.CaseType)
        //        .ToSingleObject<pPreCaseAccepRange>();
        //}

        /// <summary>
        /// 获取预立案查询
        /// </summary>
        /// <param name="PreCaseSerial"></param>
        /// <param name="QueryPWD"></param>
        /// <returns></returns>
        public PreQueryResult GetPreCaseInfo(string PreCaseSerial, int userID )
        {
            PreQueryResult result = new PreQueryResult();
            pPreCaseInfo caseInfo = NBLData
                .Select(LGCourtColumns.pPreCaseInfo)
                .Where(LGCourtColumns.pPreCaseInfo.PreCaseSerial == PreCaseSerial && LGCourtColumns.pPreCaseInfo.UserID == userID)
                .ToSingleObject<pPreCaseInfo>();
            if (caseInfo == null)
                return null;
            result.PreCaseSerial = caseInfo.PreCaseSerial;
            result.AddDateTime = caseInfo.AddDateTime.GetValueOrDefault();
            result.AppointmentDate = caseInfo.AddDateTime.GetValueOrDefault();
            result.HandleStatusName = caseInfo.Audited.Equals(1) ? "已处理" : "未处理";

            return result;
        }

        /// <summary>
        /// 案由列表
        /// </summary>
        /// <returns></returns>
        public CauseOfActionList GetCauseOfActionList()
        {
            CauseOfActionList causeOfActionList = new CauseOfActionList();
            IEnumerable<pCauseOfAction> list = NBLData
                 .Select(LGCourtColumns.pCauseOfAction)
                 .ToList<pCauseOfAction>();
            causeOfActionList.FirstList = list.Where(a => a.Level == 1).ToList();
            causeOfActionList.SecondList = list.Where(a => a.Level == 2).ToList();
            causeOfActionList.ThirdList = list.Where(a => a.Level == 3).ToList();
            causeOfActionList.FourthList = list.Where(a => a.Level == 4).ToList();
            return causeOfActionList;
        }

        /// <summary>
        /// 新增立案
        /// </summary>
        /// <param name="preCase"></param>
        /// <returns></returns>
        public string CreatePreCase(PreCassCollection preCase)
        {
            string caseSerial = DateTime.Now.ToString("yyyyMMddHHmmssfff");

            string causeOfAction = preCase.PreCaseInfo.CauseOfAction;
            string replyContent = "";
            pCauseOfAction causeOfActionInfo = NBLData
               .Select(LGCourtColumns.pCauseOfAction)
               .Where(LGCourtColumns.pCauseOfAction.CauseOfActionID == preCase.PreCaseInfo.CauseOfActionID)
               .ToSingleObject<pCauseOfAction>();
            if (causeOfActionInfo != null)
            {
                causeOfAction = string.IsNullOrEmpty(causeOfAction) ? causeOfActionInfo.CauseOfAction : causeOfAction;
            }


            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pPreCaseInfo)
                .AddColumn(LGCourtColumns.pPreCaseInfo.UserID, preCase.PreCaseInfo.UserID)
                .AddColumn(LGCourtColumns.pPreCaseInfo.CauseOfAction, causeOfAction)
                .AddColumn(LGCourtColumns.pPreCaseInfo.CauseOfActionID, preCase.PreCaseInfo.CauseOfActionID)
                .AddColumn(LGCourtColumns.pPreCaseInfo.PreCaseSerial, caseSerial)
                //.AddColumn(LGCourtColumns.pPreCaseInfo.QueryPWD, SecurityHelper.MD5(preCase.PreCaseInfo.QueryPWD, 32))
                .AddColumn(LGCourtColumns.pPreCaseInfo.Phone, preCase.PreCaseInfo.Phone)
                .AddColumn(LGCourtColumns.pPreCaseInfo.Email, preCase.PreCaseInfo.Email)
                .AddColumn(LGCourtColumns.pPreCaseInfo.PreCaseClass, preCase.PreCaseInfo.PreCaseClass)
                .AddColumn(LGCourtColumns.pPreCaseInfo.PreCaseType, preCase.PreCaseInfo.PreCaseType)
                .AddColumn(LGCourtColumns.pPreCaseInfo.ReplyContent, replyContent)
                .AddColumn(LGCourtColumns.pPreCaseInfo.AddDateTime, DateTime.Now)
                .AddColumn(LGCourtColumns.pPreCaseInfo.HandleStatus, 0)
                 .AddColumn(LGCourtColumns.pPreCaseInfo.Audited, 0);
            int ri = insert.Execute();

            if(preCase.PrePartiesList != null)
            //当事人信息
            foreach (pPreParties info in preCase.PrePartiesList)
            {
                try
                {
                    InsertSqlSection insertParties = NBLData.Insert(LGCourtColumns.pPreParties)
                       .AddColumn(LGCourtColumns.pPreParties.PreCaseSerial, caseSerial)
                        .AddColumn(LGCourtColumns.pPreParties.PartiesName, info.PartiesName)
                        .AddColumn(LGCourtColumns.pPreParties.PartiesType, info.PartiesType)
                        .AddColumn(LGCourtColumns.pPreParties.PartiesClass, info.PartiesClass)
                        .AddColumn(LGCourtColumns.pPreParties.LegalPerson, info.LegalPerson)
                        .AddColumn(LGCourtColumns.pPreParties.IdentityCard, info.IdentityCard)
                        .AddColumn(LGCourtColumns.pPreParties.Nationaly, info.Nationaly)
                        .AddColumn(LGCourtColumns.pPreParties.National, info.National)
                        .AddColumn(LGCourtColumns.pPreParties.Political, info.Political)
                        .AddColumn(LGCourtColumns.pPreParties.Birth, info.Birth)
                        .AddColumn(LGCourtColumns.pPreParties.Domicile, info.Domicile)
                        .AddColumn(LGCourtColumns.pPreParties.Degree, info.Degree)
                        .AddColumn(LGCourtColumns.pPreParties.PostalCode, info.PostalCode)
                        .AddColumn(LGCourtColumns.pPreParties.Phone1, info.Phone1)
                        .AddColumn(LGCourtColumns.pPreParties.Phone2, info.Phone2)
                        .AddColumn(LGCourtColumns.pPreParties.Address, info.Address);
                    insertParties.Execute();
                }
                catch
                {
                }
            }

            if(preCase.PreCaseFile != null)
            //文件新增
            foreach (pPreCaseFile info in preCase.PreCaseFile)
            {
                try
                {
                    InsertSqlSection insertFile = NBLData.Insert(LGCourtColumns.pPreCaseFile)
                       .AddColumn(LGCourtColumns.pPreCaseFile.PreCaseSerial, caseSerial)
                        .AddColumn(LGCourtColumns.pPreCaseFile.FileContent, info.FileContent)
                        .AddColumn(LGCourtColumns.pPreCaseFile.FileName, info.FileName)
                        .AddColumn(LGCourtColumns.pPreCaseFile.FileIntroduction, info.FileIntroduction);
                    insertFile.Execute();
                }
                catch
                {
                }
            }
            if (ri <= 0)
                return "";
            return caseSerial;
        }

        /// <summary>
        /// 删除预立案信息
        /// </summary>
        /// <param name="causeOfAction"></param>
        /// <returns></returns>
        public bool DelPreCase(pPreCaseInfo causeOfAction)
        {
            bool result = false;
            if (causeOfAction.CauseOfActionID <= 0)
                return result;
            pPreCaseInfo caseInfo = NBLData
             .Select(LGCourtColumns.pPreCaseInfo)
             .Where(LGCourtColumns.pPreCaseInfo.PreCaseInfoID == causeOfAction.PreCaseInfoID)
             .ToSingleObject<pPreCaseInfo>();
            if (caseInfo != null)
            {
                DeleteSqlSection deleteParties = NBLData.Delete(LGCourtColumns.pPreParties)
                    .Where(LGCourtColumns.pPreParties.PreCaseSerial == caseInfo.PreCaseSerial);
                deleteParties.Execute();

                DeleteSqlSection deleteCaseInfo = NBLData.Delete(LGCourtColumns.pPreCaseInfo)
                     .Where(LGCourtColumns.pPreCaseInfo.CauseOfActionID == caseInfo.CauseOfActionID);
                int ri = deleteCaseInfo.Execute();
                if (ri > 0)
                    result = true;
            }

            return result;
        }

        /// <summary>
        /// 获取当事人的预立案列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetPreCaseList(PreCaseParam condition)
        {
            PageResult result = new PageResult();
            ConditionCollection cdtColl = new ConditionCollection();
            WhereClip where = LGCourtColumns.pPreCaseInfo.UserID==condition.UserID;
            //if (!string.IsNullOrEmpty(condition.KeyWord))
            //    where = where && (LGCourtColumns.pPreCaseInfo.CauseOfAction.Contains(condition.KeyWord));

            result.ResultList = NBLData.Select(LGCourtColumns.pPreCaseInfo)
               .Where(where)
               .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.pPreCaseInfo.PreCaseInfoID)
               .OrderBy(LGCourtColumns.pPreCaseInfo.AddDateTime.Desc)
               .ToList<pPreCaseInfo>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.pPreCaseInfo, LGCourtColumns.pPreCaseInfo.PreCaseInfoID, null);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        /// <summary>
        /// 获取待审核的列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetUnAuditePreCase(PreCaseParam condition)
        {
            PageResult result = new PageResult();
            ConditionCollection cdtColl = new ConditionCollection();
            WhereClip where = WhereClip.All;
            if (!string.IsNullOrEmpty(condition.KeyWord))
                where = where && (LGCourtColumns.pPreCaseInfo.CauseOfAction.Contains(condition.KeyWord));
            if (condition.Audited.HasValue)
                where = where && LGCourtColumns.pPreCaseInfo.Audited == condition.Audited;

            result.ResultList = NBLData.Select(LGCourtColumns.pPreCaseInfo)
               .Where(where)
               .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.pPreCaseInfo.PreCaseInfoID)
               .OrderBy(LGCourtColumns.pPreCaseInfo.AddDateTime.Desc)
               .ToList<pPreCaseInfo>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.pPreCaseInfo, LGCourtColumns.pPreCaseInfo.PreCaseInfoID, null);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public PreCassCollection GetPreCaseInfo(pPreCaseInfo param)
        {
            PreCassCollection result = new PreCassCollection();
            pPreCaseInfo caseInfo = NBLData
              .Select(LGCourtColumns.pPreCaseInfo)
              .Where(LGCourtColumns.pPreCaseInfo.PreCaseInfoID == param.PreCaseInfoID)
              .ToSingleObject<pPreCaseInfo>();
            result.PreCaseInfo = caseInfo;
            if (caseInfo != null)
            {
                result.PrePartiesList = NBLData.Select(LGCourtColumns.pPreParties)
                   .Where(LGCourtColumns.pPreParties.PreCaseSerial == caseInfo.PreCaseSerial)
                   .ToList<pPreParties>();

                ExpressionClip[] fileColumns =
                 new ExpressionClip[] { 
                    LGCourtColumns.pPreCaseFile.FileName,
                    LGCourtColumns.pPreCaseFile.PreCaseFileID,
                    LGCourtColumns.pPreCaseFile.PreCaseSerial,  
                    LGCourtColumns.pPreCaseFile.FileIntroduction, 
                 };
                result.PreCaseFile = NBLData.Select(LGCourtColumns.pPreCaseFile, fileColumns)
                   .Where(LGCourtColumns.pPreCaseFile.PreCaseSerial == caseInfo.PreCaseSerial)
                   .ToList<pPreCaseFile>();
            }
            return result;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="codition"></param>
        /// <returns></returns>
        public bool AuditPreCase(pPreCaseInfo codition)
        {
            bool result = false;

            UpdateSqlSection update = NBLData.Update(LGCourtColumns.pPreCaseInfo)
                .AddColumn(LGCourtColumns.pPreCaseInfo.Audited, 1)
                .Where(LGCourtColumns.pPreCaseInfo.PreCaseInfoID == codition.PreCaseInfoID);
            int count = update.Execute();
            if (count > 0)
                result = true;
            return result;
        }

        /// <summary>
        /// 新增案由
        /// </summary>
        /// <param name="causeOfAction"></param>
        /// <returns></returns>
        public bool AddCauseOfAction(pCauseOfAction causeOfAction)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pCauseOfAction)
                .AddColumn(LGCourtColumns.pCauseOfAction.CauseOfAction, causeOfAction.CauseOfAction)
                .AddColumn(LGCourtColumns.pCauseOfAction.ParentID, causeOfAction.ParentID.GetValueOrDefault(0))
                .AddColumn(LGCourtColumns.pCauseOfAction.Level, causeOfAction.Level.GetValueOrDefault(1));

            int ri = insert.Execute();
            if (ri > 0)
                result = true;
            return result;
        }

        /// <summary>
        /// 修改案由
        /// </summary>
        /// <param name="causeOfAction"></param>
        /// <returns></returns>
        public bool EditCauseOfAction(pCauseOfAction causeOfAction)
        {
            bool result = false;
            if (causeOfAction.CauseOfActionID <= 0)
                return result;
            UpdateSqlSection update = NBLData.Update(LGCourtColumns.pCauseOfAction)
                 .AddColumn(LGCourtColumns.pCauseOfAction.CauseOfAction, causeOfAction.CauseOfAction)
                .AddColumn(LGCourtColumns.pCauseOfAction.ParentID, causeOfAction.ParentID.GetValueOrDefault(0))
                .AddColumn(LGCourtColumns.pCauseOfAction.Level, causeOfAction.Level.GetValueOrDefault(1))
                .Where(LGCourtColumns.pCauseOfAction.CauseOfActionID == causeOfAction.CauseOfActionID);

            int ri = update.Execute();
            if (ri > 0)
                result = true;
            return result;
        }

        /// <summary>
        /// 删除案由
        /// </summary>
        /// <param name="causeOfAction"></param>
        /// <returns></returns>
        public bool DelCauseOfAction(pCauseOfAction causeOfAction)
        {
            bool result = false;
            if (causeOfAction.CauseOfActionID <= 0)
                return result;
            DeleteSqlSection delete = NBLData.Delete(LGCourtColumns.pCauseOfAction)
                 .Where(LGCourtColumns.pCauseOfAction.CauseOfActionID == causeOfAction.CauseOfActionID);

            int ri = delete.Execute();
            if (ri > 0)
                result = true;
            return result;
        }

        /// <summary>
        /// 修改案件受理范围
        /// </summary>
        /// <param name="caseAccepRange"></param>
        /// <returns></returns>
        public bool EditCaseAccepRange(CaseAccepRangeParam caseAccepRange)
        {
            bool result = false;
            if (string.IsNullOrEmpty(caseAccepRange.RangeContent))
                return result;

            //先删除，再新增
            DeleteSqlSection delete = NBLData.Delete(LGCourtColumns.pPreCaseAccepRange)
                                    .Where(LGCourtColumns.pPreCaseAccepRange.CaseType == caseAccepRange.CaseType);
            delete.Execute();

            InsertSqlSection insert = NBLData.Insert(LGCourtColumns.pPreCaseAccepRange)
                .AddColumn(LGCourtColumns.pPreCaseAccepRange.RangeContent, caseAccepRange.RangeContent)
                .AddColumn(LGCourtColumns.pPreCaseAccepRange.CaseType, caseAccepRange.CaseType);

            int ri = insert.Execute();
            if (ri > 0)
                result = true;
            return result;
        }

    }

    public class PreCaseParam : PageParam
    {
        public string KeyWord { get; set; }
        public short? Audited { get; set; }
        public int UserID { get; set; }
    }

    public class PreCassCollection
    {
        public pPreCaseInfo PreCaseInfo { get; set; }
        public IEnumerable<pPreParties> PrePartiesList { get; set; }
        public IEnumerable<pPreCaseFile> PreCaseFile { get; set; }
    }

    public class CauseOfActionList
    {
        public List<pCauseOfAction> FirstList { get; set; }
        public List<pCauseOfAction> SecondList { get; set; }
        public List<pCauseOfAction> ThirdList { get; set; }
        public List<pCauseOfAction> FourthList { get; set; }
    }

    public class PreQueryResult
    {
        public string PreCaseSerial { get; set; }
        public string HandleStatusName { get; set; }
        public DateTime AddDateTime { get; set; }
        public DateTime AppointmentDate { get; set; }
        public string AutoReplyContent { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class CaseAccepRangeParam
    {
        public string CaseType { get; set; }
        public string RangeContent { get; set; }
    }

    public static class CaseTypes
    {
        /// <summary>
        /// 民事案件受理范围
        /// </summary>
        public static string MinShi
        {
            get
            {
                return "MinShi";
            }
        }
        /// <summary>
        /// 商事案件受理范围
        /// </summary>
        public static string ShangShi
        {
            get
            {
                return "ShangShi";
            }
        }
        /// <summary>
        /// 刑事案件受理范围
        /// </summary>
        public static string XingShi
        {
            get
            {
                return "XingShi";
            }
        }
        /// <summary>
        /// 执行案件受理范围
        /// </summary>
        public static string ZhiXing
        {
            get
            {
                return "ZhiXing";
            }
        }
    }

}
